package com.ada.controller;

import com.ada.dao.ResumeDao;
import com.ada.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    ResumeDao resumeDao;

    @RequestMapping("/all")
    @ResponseBody
    public List<Resume> handlerAll() {
        List<Resume> all = resumeDao.findAll();
        return all;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Resume handlerAdd(@RequestBody Resume resume) {
        if(resume.getId()==null&&resume.getAddress().isEmpty()&&resume.getName().isEmpty()&&resume.getPhone().isEmpty()){
            return null;
        }
        Resume save = resumeDao.save(resume);
        return save;
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Resume handlerDelete(@PathVariable("id")long id) {
        Resume resume = new Resume();
        resume.setId(id);
        resumeDao.delete(resume);
        return resume;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Resume handlerUpdate(@RequestBody Resume resume) {
        resume=resumeDao.save(resume);
        return resume;
    }

    @RequestMapping("/login")
    @ResponseBody
    public String handlerlogin() {
        return "success";
    }


    @RequestMapping("/logout")
    @ResponseBody
    public String handlerlogout() {
        return "success";
    }
}
